package com.ybxx.core.util;

public class MapUtil {
	public static double DEF_PI = 3.14159265359; // PI
	public static double DEF_2PI = 6.28318530712; // 2*PI
	public static double DEF_PI180 = 0.01745329252; // PI/180.0
	public static double DEF_R = 6370693.5; // radius of earth
	public static double DEF_R_KM = 6370.6935;

	/**
	 * 两点坐标计算距离
	 * @param sln 源经度
	 * @param slat 源纬度
	 * @param dln 目的经度
	 * @param dlat 目的纬度
	 * @return
	 */
	public static Double getDistance(double sln, double slat, double dln,
			double dlat) {
		double distance;
		// 角度转换为弧度
		sln = sln * DEF_PI180;
		slat = slat * DEF_PI180;
		dln = dln * DEF_PI180;
		dlat = dlat * DEF_PI180;
		// 求大圆劣弧与球心所夹的角(弧度)
		distance = Math.sin(slat) * Math.sin(dlat) + Math.cos(slat)
				* Math.cos(dlat) * Math.cos(sln - dln);
		// 调整到[-1..1]范围内，避免溢出
		if (distance > 1.0)
			distance = 1.0;
		else if (distance < -1.0)
			distance = -1.0;
		// 求大圆劣弧长度
		distance = DEF_R * Math.acos(distance);
		return distance;
	}
	
	public static void main(String[] args){
		System.out.println(MapUtil.getDistance(108.901779, 34.205078, 108.913619, 34.240032));
	}
}
